package com.weisy.signin.ui.activity;

import android.content.DialogInterface;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.weisy.signin.R;
import com.weisy.signin.adapter.CommAdapter;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.Plan;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.widget.MsgDialog;
import com.yinshan.calendar.PopCalendar;
import com.yinshan.calendar.utils.DateTimeHelper;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 计划列表
 * @date 2019/3/28 8:14
 */
@Route(path = Comm.ROUTE_PLAN_LIST)
public class PlanListActivity extends BaseAcativity {

    @BindView(R.id.line_no_data)
    LinearLayout lineNull;
    @BindView(R.id.list_plan)
    ListView listPlan;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.txt_plan_time)
    TextView txtTime;

    private CommAdapter<Plan> adapter;
    private ArrayList<Plan> plans = new ArrayList<>();

    @Autowired
    public String day;

    private PopCalendar popCalendar;

    @Override
    public void findView() {
        setContentView(R.layout.activity_plan_list);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void initView() {
        if (TextUtils.isEmpty(day)) {
            day = DateTimeHelper.formatToString(Calendar.getInstance().getTime(), DateTimeHelper.FORMAT_DAY);
        }
        txtTime.setText(day);
        adapter = new CommAdapter<Plan>(plans, R.layout.layout_item_plan_list) {
            @Override
            public void bindView(ViewHolder holder, Plan obj) {
                holder.setText(R.id.txt_item_plan_title, obj.getTitle());
                holder.setText(R.id.txt_item_plan_time, "时间:" + obj.getDate());
                holder.setText(R.id.txt_item_plan_local, "地点:" + obj.getLocal());

            }
        };
        listPlan.setAdapter(adapter);
        listPlan.setOnItemClickListener((parent, view1, position, id) -> {
            ARouter.getInstance().build(Comm.ROUTE_PLAN_INFO).withParcelable("plan", plans.get(position)).navigation();
        });
        listPlan.setOnItemLongClickListener((parent, view12, position, id) -> {
            showDialog(plans.get(position));
            return false;
        });
        isResume = true;
        refreshLayout.setOnRefreshListener(refreshLayout -> initData());

        popCalendar = new PopCalendar(context)
                .setSelectColor(R.color.colorBlue)
                .setOnResultListener(new PopCalendar.OnResultListener() {
                    @Override
                    public void onResult(int i, int i1, int i2) {
                        Calendar c = Calendar.getInstance();
                        c.set(Calendar.YEAR, i);
                        c.set(Calendar.MONTH, i1 - 1);
                        c.set(Calendar.DAY_OF_MONTH, i2);
                        day = DateTimeHelper.formatToString(c.getTime(), DateTimeHelper.FORMAT_DAY);
                        txtTime.setText(day);
                        refreshLayout.autoRefresh();
                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    @Override
    public void initData() {
        List<Plan> tmp = LitePal.where("date=?", day).find(Plan.class);
        plans.clear();
        if (null != tmp && !tmp.isEmpty()) {
            plans.addAll(tmp);
            adapter.notifyDataSetChanged();
        }
        if (plans.isEmpty()) {
            lineNull.setVisibility(View.VISIBLE);
        } else {
            lineNull.setVisibility(View.GONE);
        }
        new Handler().postDelayed(() -> refreshLayout.finishRefresh(), 1000);
    }

    @OnClick({R.id.txt_plan_time, R.id.img_add_close})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_add_close:
                finish();
                break;
            case R.id.txt_plan_time:
                if (null != popCalendar) {
                    popCalendar.show(view);
                }
                break;
        }
    }

    private void showDialog(Plan plan) {
        new MsgDialog.Builder(context)
                .setCenter(true)
                .setMsg("您确定要删除该条记录吗？")
                .setBtnColor(ContextCompat.getColor(context, R.color.colorBlue))
                .setTxtLeft("取消", (dialog, which) -> dialog.dismiss())
                .setTxtRight("确定", (DialogInterface.OnClickListener) (dialog, which) -> {
                    dialog.dismiss();
                    if (plan.delete() > 0) {
                        ToastUtils.show("删除成功");
                        initData();
                    } else {
                        ToastUtils.show("删除失败");
                    }
                }).create().show();
    }

}
